﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using JoystickInterface;
using System.Xml;
using System.IO;

namespace JoypadParser
{
    public partial class JoypadSettingsForm : Form
    {
        private Joystick _joystick;

        public JoypadSettingsForm(Joystick joystick)
        {
            InitializeComponent();
            _joystick = joystick;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _modelSettings.Text = "Settings For " + _joystick.Model;

            Type type = _joystick.GetType();

            foreach (PropertyInfo prop in type.GetProperties())
            {
                //prendo tutte le property della classe joystick accessibili solo in get
                if (prop.CanRead && !prop.CanWrite)
                {
                    foreach (Attribute item in prop.GetCustomAttributes(true))
                    {
                        _axisJoyA.Items.Add(prop.Name);
                        _axisJoyB.Items.Add(prop.Name);
                        _axisJoyC.Items.Add(prop.Name);
                        _axisJoyD.Items.Add(prop.Name);
                    }
                }
            }

            //se esistono già delle impostazioni per quel joypad, le faccio vedere nella grafica
            XmlDocument document = new XmlDocument();
            string filename = Constants.GetSettingsFile(_joystick);
            try
            {
                int count = 0;
                document.Load(filename);
                foreach (XmlNode node in document.SelectNodes("/joypad/asse"))
                {
                    if (count == 0)
                    {
                        _axisJoyA.Text = node.FirstChild.InnerText;
                        _isInvertedA.Checked = XmlConvert.ToBoolean(node.Attributes["inverted"].Value);
                    }
                    if (count == 1)
                    {
                        _axisJoyB.Text = node.FirstChild.InnerText;
                        _isInvertedB.Checked = XmlConvert.ToBoolean(node.Attributes["inverted"].Value);
                    }
                    if (count == 2)
                    {
                        _axisJoyC.Text = node.FirstChild.InnerText;
                        _isInvertedC.Checked = XmlConvert.ToBoolean(node.Attributes["inverted"].Value);
                    }
                    if (count == 3)
                    {
                        _axisJoyD.Text = node.FirstChild.InnerText;
                        _isInvertedD.Checked = XmlConvert.ToBoolean(node.Attributes["inverted"].Value);
                    }
                    count++;
                }
            }
            catch (FileNotFoundException)
            {
            }

        }

        //i profili dei joystick vengono salvato con un nome univoco creato partendo da nome del modello.
        //vengono salvati in una cartella predefinita nel project JoystickInterface.
        private void _salvaBtn_Click(object sender, EventArgs e)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.Encoding = Encoding.ASCII;
            string filename = Constants.GetSettingsFile(_joystick);

            XmlWriter writer = XmlWriter.Create(filename, settings);
            writer.WriteStartDocument();
            writer.WriteStartElement("joypad"); //JOYPAD

            writer.WriteStartElement("asse");
            writer.WriteAttributeString("inverted", XmlConvert.ToString(_isInvertedA.Checked));
            writer.WriteString(_axisJoyA.SelectedItem.ToString());
            writer.WriteEndElement();


            writer.WriteStartElement("asse");
            writer.WriteAttributeString("inverted", XmlConvert.ToString(_isInvertedB.Checked));
            writer.WriteString(_axisJoyB.SelectedItem.ToString());
            writer.WriteEndElement();


            writer.WriteStartElement("asse");
            writer.WriteAttributeString("inverted", XmlConvert.ToString(_isInvertedC.Checked));
            writer.WriteString(_axisJoyC.SelectedItem.ToString());
            writer.WriteEndElement();


            writer.WriteStartElement("asse");
            writer.WriteAttributeString("inverted", XmlConvert.ToString(_isInvertedD.Checked));
            writer.WriteString(_axisJoyD.SelectedItem.ToString());
            writer.WriteEndElement();

            writer.WriteEndElement(); // chiudo JOYPAD

            writer.WriteEndDocument();
            writer.Close();

            // COME ULTIMA COSA SETTO LE OPZIONI DEL JOYSTICK
            PropertyInfo[] set = new PropertyInfo[4];
            Type type = _joystick.GetType();

            string prop = _axisJoyA.SelectedItem.ToString();
            set[0] = type.GetProperty(prop);

            prop = _axisJoyB.SelectedItem.ToString();
            set[1] = type.GetProperty(prop);

            prop = _axisJoyC.SelectedItem.ToString();
            set[2] = type.GetProperty(prop);

            prop = _axisJoyD.SelectedItem.ToString();
            set[3] = type.GetProperty(prop);

            _joystick.Settings = set;

            //setto i boolean per i valori invertiti
            bool[] inv = new bool[4];
            inv[0] = _isInvertedA.Checked;
            inv[1] = _isInvertedB.Checked;
            inv[2] = _isInvertedC.Checked;
            inv[3] = _isInvertedD.Checked;

            _joystick.InvertedAxis = inv;

            this.Close();
        }

    }
}
